﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using Reconciliation.Services;
using System.Data;
using System.Xml.Serialization;
using System.IO;
using System.Text;

namespace Reconciliation.Web
{
    public partial class ReconciliationStatus : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FilterControl.PageName = "Status";
            FilterControl.LoadVariables();
            AddAjaxSettings();

            bindGrid();
        }

        private void bindGrid()
        {
            RadGrid1.DataSource = Services.Services.GetOrderList(FilterControl.StartDate, FilterControl.EndDate);
            RadGrid1.DataBind();
            BasePage.Grid = RadGrid1;
        }

        private void AddAjaxSettings()
        {
            /** add controls top ajax panerl **/
            ImageButton imgButton = (ImageButton)FilterControl.FindControl("imgApply");
            //Add the necessary AJAX setting programmatically
            RadAjaxManager1.AjaxSettings.AddAjaxSetting(imgButton, RadGrid1, RadAjaxLoadingPanel1);

            DropDownList ddCCP = (DropDownList)FilterControl.FindControl("ddCCP");
            RadAjaxManager1.AjaxSettings.AddAjaxSetting(ddCCP, RadGrid1, RadAjaxLoadingPanel1);

            DropDownList ddAmountFilter = (DropDownList)FilterControl.FindControl("ddAmountFilter");
            RadAjaxManager1.AjaxSettings.AddAjaxSetting(ddAmountFilter, RadGrid1, RadAjaxLoadingPanel1);

            TextBox txtAmount = (TextBox)FilterControl.FindControl("txtAmount");
            RadAjaxManager1.AjaxSettings.AddAjaxSetting(txtAmount, RadGrid1, RadAjaxLoadingPanel1);

            //RadAjaxLoadingPanel AjaxPanel2 = (RadAjaxLoadingPanel)FilterControl.FindControl("RadAjaxLoadingPanel2");
            //LinkButton lnkButton = (LinkButton)FilterControl.FindControl("lnkFilter");
            //Panel lnlFilter = (Panel)FilterControl.FindControl("pnlFilter");
            //RadAjaxManager1.AjaxSettings.AddAjaxSetting(lnkButton, lnlFilter, AjaxPanel2);
            
            DropDownList ddBank_Status = (DropDownList)FilterControl.FindControl("ddBank_Status");
            RadAjaxManager1.AjaxSettings.AddAjaxSetting(ddBank_Status, RadGrid1, RadAjaxLoadingPanel1);
            /** end ajsx panel **/
        }
              

        protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            RadGrid1.DataSource = Services.Services.GetOrderList(FilterControl.StartDate, FilterControl.EndDate);
        }

        protected void OnAjaxUpdate(object sender, ToolTipUpdateEventArgs args)
        {
            this.UpdateToolTip(args.Value, args.UpdatePanel);
            
        }

        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {            
            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;
                Control image = e.Item.FindControl("lnkClientComment");
                HiddenField hddCaseID = (HiddenField)e.Item.FindControl("hiddCaseID");
                                
                Control image2 = e.Item.FindControl("lnkAuditorComment");

                string CommentId = hddCaseID.Value.ToString();
                this.RadToolTipManager1.TargetControls.Add(image.ClientID, CommentId + "@Client", true);
                this.RadToolTipManager2.TargetControls.Add(image2.ClientID, CommentId + "@Auditor", true);

                /** add view details popup **/

                HyperLink lnkDetails = (HyperLink)e.Item.FindControl("lnkViewDetails");
                lnkDetails.Attributes.Add("onclick", "viewCaseViewDetails('" + lnkDetails.Text + "');");

            }
        }

        private void UpdateToolTip(string elementID, UpdatePanel panel)
        {

            Control ctrl = Page.LoadControl("~/Users Control/StatusComments.ascx");
            panel.ContentTemplateContainer.Controls.Add(ctrl);

            Reconciliation.Web.Users_Control.StatusComments comments = (Reconciliation.Web.Users_Control.StatusComments)ctrl;

            string[] elements = elementID.Split('@');

            comments.CaseID = int.Parse(elements[0].ToString());
            comments.CommentType = elements[1].ToString();
        }

       
}
}
